package com.qianbao.qianbaobusiness.ui.cashier.databill;

import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.qianbao.qianbaobusiness.R;


/**
 * Created by hdb on 2016/11/17.
 */
public class ToolBarHelper {

    /*上下文，创建view的时候需要用到*/
    private Context mContext;
    /*base view*/
    private CoordinatorLayout mContentView;
    /*用户定义的view*/
    private View mUserView;
    /*toolbar*/
    private Toolbar mToolBar;
    /*视图构造器*/
    private LayoutInflater mInflater;

//    private TextView mTvLeft;

    public ToolBarHelper(Context context, int layoutId) {
        this.mContext = context;
        mInflater = LayoutInflater.from(mContext);
        /*初始化整个内容*/
        initContentView();
        /*初始化toolbar*/
        initToolBar();
        /*初始化用户定义的布局*/
        initUserView(layoutId);
    }

    private void initContentView() {
        /*直接创建一个帧布局，作为视图容器的父容器*/
        mContentView = new CoordinatorLayout(mContext);
//        mContentView.setFitsSystemWindows(true);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.setLayoutParams(params);
    }

    private void initToolBar() {
        /*通过inflater获取toolbar的布局文件*/
        View toolbar = mInflater.inflate(R.layout.toolbar, mContentView);
        mToolBar = (Toolbar) toolbar.findViewById(R.id.toolbar);
//        mTvLeft = (TextView)toolbar.findViewById(R.id.tv_left);
    }

    private void initUserView(int id) {
        mUserView = mInflater.inflate(id, null);
        CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
        mContentView.addView(mUserView, params);
    }

    public void setToolBarSize(int pixels) {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mToolBar.getLayoutParams();
        params.height = pixels;
        mToolBar.setLayoutParams(params);
    }

    public CoordinatorLayout getContentView() {
        return mContentView;
    }

    public Toolbar getToolBar() {
        return mToolBar;
    }
}
